package com.aaa.phone.service.resetpasswordservice.impl;

import com.aaa.entity.AppUserlogin;
import com.aaa.phone.dao.logindao.ILoginDao;
import com.aaa.phone.resultcommon.Result;
import com.aaa.phone.dao.resetpassworddao.IResetPasswordDao;
import com.aaa.phone.service.resetpasswordservice.IResetPasswordService;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.web.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @program: batterysharing
 * @description: 用户更改密码
 * @author: 王璐
 * @create: 2018-12-15 16:34
 **/
@Service
public class ResetPasswordServiceImpl implements IResetPasswordService,ISystemConst {

    @Autowired
    private IResetPasswordDao dao;
    @Autowired
    private ILoginDao loginDao;

    /**
     * 用户更改密码
     * @param tel
     * @param psw
     * @return
     */
    @Override
    public int resetPsw(String tel, String psw) {

        int exec = dao.selectTel(tel);
        Result result = new Result();
        //判断手机号是否存在,返回一个对象
        AppUserlogin userExist = loginDao.userExist(tel);
        //判断手机号是否存在
        if (exec > 0) {
            //给密码加盐
            String password = ShiroUtil.getPassword(userExist.getSalt(),psw);
            int i = dao.updateUser(tel,password);
            if (i > 0) {
                //更改密码成功，返回成功码100
                result.setCode(SUCCESS_CODE);
            } else {
                //更改密码失败，返回失败码200
                result.setCode(DEFEAT_CODE);
            }
        }else{
            //用户手机号不存在  返回用户已注册码201
            result.setCode(USER_EXISTED);
        }
        return result.getCode();
    }
}
